const mysql = require('mysql2');
const config = require('./config');

// 创建与数据库的连接池
const connection = mysql.createPool({
  host: config.MYSQL_HOST,
  port: config.MYSQL_PORT,
  database: config.MYSQL_DATABASE,
  user: config.MYSQL_USER,
  password: config.MYSQL_PASSWORD,
});

// 与数据库进行连接
connection.getConnection((err, connection) => {
  connection.connect(err => {
    if (err) return console.log('与数据库链接失败');
    console.log(`与数据库${config.MYSQL_DATABASE}连接成功`);
  })
});

module.exports = connection.promise();